package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.Log;
import com.itheima.dao.EmpMapper;
import com.itheima.dao.ExperienceMapper;
import com.itheima.dto.SelectEmpDTO;
import com.itheima.po.*;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Experience;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    ExperienceMapper experienceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Log
    public Result deleteByIds(List<Integer> ids) {
        //先删除员工
        int i = empMapper.deleteByIds(ids);
        //再删除履历
        int i1 = experienceMapper.deleteByIds(ids);
        log.info("删除{}条履历",i1);
        return Result.success("成功删除" + i + "条数据");
    }

    @Override

    public PageVO<SelectEmpDTO> selectEmpPage(SelectEmpPageParam param) {

//        int page=(selectEmpPageParam.getPage()-1)*selectEmpPageParam.getPageSize();
//        selectEmpPageParam.setPage(page);
//        List<SelectEmpDTO> emps = empMapper.selectEmp(selectEmpPageParam);
//        Integer count = empMapper.selectEmpCount(selectEmpPageParam);
//        Page<List<SelectEmpDTO>> pageEmp = new Page<>();
//        pageEmp.setTotal(count);
//        pageEmp.setRows(emps);

        //注意pageHelper会自动执行一个统计count sql ,并且会自动拼接limit(一定要把自己sql中的limit去掉!)
        //因此必须写在自己的sql分页之前
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectEmpDTO> emps = empMapper.selectEmp(param);
        PageVO<SelectEmpDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(emps);
        return pageVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addEmp(InsertEmp emp) {
        int i=empMapper.addEmp(emp);
        log.info("员工的主键{}",emp.getId());
        if (CollectionUtils.isNotEmpty(emp.getExprList())) {
            //赋值员工id以及时间
            List<Experience> collect = emp.getExprList().stream().map(t -> {
                t.setEmpId(emp.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i1 = experienceMapper.batchInsertExper(collect);
            log.info("添加了{}条工作经验",i1);
        }
        return i;
    }

    @Override
    public SelectEmpById selectById(Integer id) {
        //先查询员工
        SelectEmpById emp = empMapper.selectById(id);
        //再查询履历
        List<Experience> experiences = experienceMapper.selectByEmpId(id);
        emp.setExprList(experiences);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(UpdateEmp emp) {
        //先更新员工表信息
        int i2 = empMapper.updateEmp(emp);
        emp.setUpdateTime(LocalDateTime.now());
        //再删除该员工所有履历
        int i = experienceMapper.deleteByIds(List.of(emp.getId()));
        log.info("删除几条{}旧履历数据",i);
        //新增所有履历
        if (CollectionUtils.isNotEmpty(emp.getExprList())) {
            List<Experience> collect = emp.getExprList().stream().map(t -> {
                t.setEmpId(emp.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i1 = experienceMapper.batchInsertExper(collect);
            log.info("添加了{}条新工作经验",i1);
       }
        return i2;
    }

    @Override
    public List<SelectEmpList> selectAll() {
        return empMapper.selectAll();
    }

}
